<?php

namespace app\model;

//use support\Model;
use app\controller\wmapi\IndexController;
use app\controller\wmapi\PublicCommentController;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaMonthlySettlement
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_order_monthly_settlement';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all($condition)
    {
        $condition [] = ['is_show','=',1];
        $list = Db::table('wa_order_monthly_settlement')
            ->select('*')
            ->where($condition)
            ->orderBy('id', 'asc')
            ->get();

        if ($list) {
            foreach ($list as $k){

            }
        }
        return json_encode($list);

    }



    public static function created($data)
    {
        $result = Db::table('wa_order_monthly_settlement')->insertGetId($data);
        return $result;
    }

    public static function updated($data, $where)
    {
        $result = Db::table('wa_order_monthly_settlement')->where($where)->update($data);
        return $result;
    }

    public static function deleted($where)
    {
        $result = Db::table('wa_order_monthly_settlement')->where($where)->delete();
        return $result;
    }

    public static function list($page, $pageNum, $fields = ['*'], $condition = [], $between = [])
    {
        $condition [] = ['is_show','=',1];
        $fields = [
            Db::raw("ANY_VALUE(supplier_id) as supplier_id"),
            Db::raw("ANY_VALUE(supplier_type) as supplier_type"),
            Db::raw("ANY_VALUE(supplier_name) as supplier_name"),
            Db::raw("ANY_VALUE(price_name) as price_name"),
            Db::raw("ANY_VALUE(settlement_sratus) as settlement_sratus"),
            Db::raw("ANY_VALUE(year) as year"),
            Db::raw("ANY_VALUE(month) as month"),
            Db::raw("ANY_VALUE(total_price) as total_price"),
            Db::raw("ANY_VALUE(purchase_payroll_sn) as purchase_payroll_sn"),
            "bill_number"
        ];
        $offset = ($page - 1) * $pageNum;
        if ($between) {
            $model = Db::table('wa_order_monthly_settlement')
                ->select($fields)
                ->where($condition)
                ->whereBetween('created_at', $between);
        } else {
            $model = Db::table('wa_order_monthly_settlement')
                ->select($fields)
                ->where($condition);
        }

        $list = $model
            ->orderBy('year', 'desc')
            ->orderBy('month', 'desc')
            ->groupBy('bill_number')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
        $count = count($list);

        if ($list) {
            foreach ($list as $k){

                PublicCommentController::供应商类型;
//                if ($k->supplier_type == 1) {
//                    $k->supplier_type_name = '车辆供应商';
//                } elseif ($k->supplier_type == 2) {
//                    $k->supplier_type_name = '上牌供应商';
//                } elseif ($k->supplier_type == 3) {
//                    $k->supplier_type_name = '国际货运代理';
//                } elseif ($k->supplier_type == 4) {
//                    $k->supplier_type_name = '保险供应商';
//                } elseif ($k->supplier_type == 5) {
//                    $k->supplier_type_name = '国内物流';
//                } else {
//                    $k->supplier_type_name = '';
//                }
                $k->supplier_type_name = IndexController::supplierTypeNameCommon($k->supplier_type);
                // 获取对应的款项数据
                $children_by_month_condition = [];
//                $children_by_month_condition [] = ['supplier_id','=',$k->supplier_id];
//                $children_by_month_condition [] = ['supplier_type','=',$k->supplier_type];
//                $children_by_month_condition [] = ['year','=',$k->year];
//                $children_by_month_condition [] = ['month','=',$k->month];
                $children_by_month_condition [] = ['bill_number','=',$k->bill_number];
                $children_by_month_condition [] = ['settlement_sratus','!=',4]  ;
                $children_by_month_data = WaMonthlySettlement::all($children_by_month_condition);
                if ($children_by_month_data) {
                    $k->children_by_month_data = json_decode($children_by_month_data);
                    $k->count_by_month = count($k->children_by_month_data); // 应付款笔数(应付款项笔数)

                }else{
                    $k->count_by_month = 0; // 应付款笔数(应付款项笔数)
                    $k->children_by_month_data = [];
                }
                PublicCommentController::请款单请款结算类型;
                // 获取请款状态
                $payroll_requisition_condition = [];
                $payroll_requisition_condition [] = ['bill_number','=',$k->bill_number];
                $payroll_requisition_condition [] = ['year','=',$k->year];
                $payroll_requisition_condition [] = ['month','=',$k->month];
                $payroll_requisition_condition [] = ['payroll_requisition_type','=',3]; // 请款请款
                $payroll_requisition_condition [] = ['status','<',4]; //  0待审批 1审批中 2 待付款 3 已付款 4 审核不通过 5 撤回 6 删除
                //已请款金额
                $payroll_requisition_data = WaPurchasePayrollRequisition::find($payroll_requisition_condition);
                $payroll_requisition_data = json_decode($payroll_requisition_data);
                if ($payroll_requisition_data && !empty($payroll_requisition_data)){
                    $k->requested_price = round($payroll_requisition_data->requested_price,2);// 已付账款总额

                    PublicCommentController::请款单状态;
                    if ($payroll_requisition_data->status == 1){
                        //审批状态
                        $k->approve_stauts = 1;
                        $k->approve_stauts_name = '审批中';
                        //请款状态
                        $k->payroll_requisition_status = 1; // 请款状态   0 待审批 1 审批中 2 待付款 3 已付款 4 审核不通过 5 撤回 6 删除
                        $k->payroll_requisition_status_name = '审批中';
                    }elseif ($payroll_requisition_data->status == 2){
                        $k->approve_stauts = 2;
                        $k->approve_stauts_name = '待付款';
                        $k->payroll_requisition_status = 2; // 请款状态   0 待审批 1 审批中 2 待付款 3 已付款 4 审核不通过 5 撤回 6 删除
                        $k->payroll_requisition_status_name = '待付款';
                    }elseif ($payroll_requisition_data->status == 3){
                        $k->approve_stauts = 3;
                        $k->approve_stauts_name = '已付款';
                        $k->payroll_requisition_status = 3; // 请款状态   0 待审批 1 审批中 2 待付款 3 已付款 4 审核不通过 5 撤回 6 删除
                        $k->payroll_requisition_status_name = '已付款';
                    }else{
                        $k->approve_stauts = 0;
                        $k->approve_stauts_name = '暂无该状态';
                        $k->payroll_requisition_status = 0; // 请款状态   0 待审批 1 审批中 2 待付款 3 已付款 4 审核不通过 5 撤回 6 删除
                        $k->payroll_requisition_status_name = '暂无该状态';
                    }

                    // 结算状态 1 未结清 2 结款中 3 已结清 4 撤回
//                    if ($k->settlement_sratus == 1){
//                        $k->settlement_sratus_name = '未结清';
//
//                    }elseif ($k->settlement_sratus == 2){
//                        $k->settlement_sratus_name = '结款中';
//
//                    }elseif ($k->settlement_sratus == 3){
//                        $k->settlement_sratus_name = '已结清';
//
//                    }elseif ($k->settlement_sratus == 4){
//                        $k->settlement_sratus_name = '撤回';
//                    }else{
//                        $k->settlement_sratus_name = '暂无该状态';
//                    }

                    $k->settlement_sratus_name = IndexController::billStatusName($k->settlement_sratus);



                    //审批状态  通过请款编号 找到 pr_no 审批编号 再获取对应审批的 状态
//                    if($payroll_requisition_data->pr_no){
//                        //审批流数据
//                        $payroll_approve_condition = [];
//                        $payroll_approve_condition [] = ['pr_no','=',$payroll_requisition_data->pr_no];
//                        $payroll_approve_data = WaPayrollRequisitionExamineApprove::findApproveStatusByPrNo($payroll_approve_condition);
//                        if ($payroll_approve_data){
//                            $payroll_approve_data = json_decode($payroll_approve_data);
//
//                        }
//
//                    }else{
//                        $k->payroll_requisition_status = 0;// 请款状态 ：0 未请款 1 审批中 2 待付款  3已付款
//                        $k->payroll_requisition_status_name = '未请款';
//                    }

                }else{
                    $k->requested_price = 0;// 已付账款总额
                    $k->payroll_requisition_status = 0;// 请款状态 ：0 未请款 1 审批中 2 待付款  3已付款
                    $k->payroll_requisition_status_name = '未请款';
                }

                // 审批状态 和 请款状态
                // settlement_sratus 结算状态 1 未结清 2 结款中(请款创建、作废需要修改) 3 已结清(财务付款需要修改) 4 撤回(即删除 订单中的状态需求变回待请款)
                // approve_stauts 请款单状态 ：0 待审批 1 审批中 2 待付款 3 已付款 4 审核不通过 5 撤回 6 删除
//                if ($k->settlement_sratus == 1){
//                    $k->settlement_sratus_name = '未结清';
//                }elseif ($k->settlement_sratus == 2){
//                    $k->settlement_sratus_name = '结款中';
//                }elseif ($k->settlement_sratus == 3){
//                    $k->settlement_sratus_name = '已结清';
//                }elseif ($k->settlement_sratus == 4){
//                    $k->settlement_sratus_name = '撤回/删除';
//                }else{
//                    $k->settlement_sratus_name = '暂无该结算状态';
//                }
                $k->settlement_sratus_name = IndexController::billStatusName($k->settlement_sratus);

            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }

    public static function find($condition)
    {
        $res = Db::table('wa_order_monthly_settlement')
            ->where($condition)
            ->first();
        return json_encode($res);
    }




}